/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function confirmDialog(url, message) {
    var answer = confirm(message)
    if (answer) {
        document.location.href = url;
    }
}

function href(url, message) {
    var answer = confirm(message)
    if (answer) {
        document.location.href = url;
    }
}

function isEmpty(param)  {
    var result = true;
    if (param == null || param.value == null || param.value == '')
        result = false; 
    return result;
}

function isDecimal(param)  {
    var numericExpression = /^[+-]?((\d+|\d{1,3}(\,\d{3})+)(\.\d*)?|\.\d+)$/;
    if (param.value.match(numericExpression)) {
        return true;
    }
    else {
        return false;
    }
}

function isNumber(param)  {
    var numericExpression = /^[0-9]+$/;
    if (param.value.match(numericExpression)) {
        return true;
    }
    else {
        return false;
    }
}

function isDate(data) {
    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
    if ((data.value.match(expReg)) && (data.value !='')) {
        var dia = Number(data.value.substring(0, 2));
        var mes = Number(data.value.substring(3, 5));
        var ano = Number(data.value.substring(6, 10));
        if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30) {
            return false;
        } 
        else {
            if (ano % 4 != 0 && mes == 2 && dia > 28) {
                return false;
            } 
            else {
                if (ano % 4 == 0 && mes == 2 && dia > 29) {
                    return false;
                } 
                else {
                    return true;
                }
            }
        }
    } 
    else {
        return false;
    }
}    

function onlyNumbers(event) {
    if (event.keyCode >= 48 && event.keyCode <= 57)
        return true;
    else
        return false;
}

function formatDate(field, event){
    separator = "/"; 
    event = event.keyCode; 
    value = field.value.split(''); 
    result = ""; 
    i = 0;
    while (i < value.length) {
        character = value[i];
        numbers = /^\d+$/;
        if (numbers.test(character) || character == separator) {
            if (character == separator) {
                if (i == 2 || i == 5)
                    result += String(character);
            }
            else {
                result += String(character);
            }
        }
        if ((result.length == 2 || result.length == 5) && event != 8) {
            result += separator; 
            i++;
        }
        i++;
    }
    field.value = result;
}

function getJsonGet(url) {
    var json = null;
    new Ajax.Request(url, 
    {
        method: 'get',
        requestHeaders: {Accept: 'application/json'},
        asynchronous: false,
        onSuccess: function (transport) {
            json = transport.responseText.evalJSON(true);
        },
        onFailure: function () {
            alert('Error');
        }
    });
    return json;
}
    
function getXmlGet(url) {
    var json = null;
    new Ajax.Request(url, 
    {
        method: 'get',
        requestHeaders: {Accept: 'application/xml'},
        asynchronous: false,
        onSuccess: function (transport) {
            json = transport.responseText;
        },
        onFailure: function () {
            alert('Error');
        }
    });
    return json;
}